#include "newnumberkey.h"
#include "ui_newnumberkey.h"
#include <QLineEdit>



NewNumberKey::NewNumberKey(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NewNumberKey)
{
    ui->setupUi(this);
    ui->btn_0->setProperty("key",'0');
    ui->btn_1->setProperty("key",'1');
    ui->btn_2->setProperty("key",'2');
    ui->btn_3->setProperty("key",'3');
    ui->btn_4->setProperty("key",'4');
    ui->btn_5->setProperty("key",'5');
    ui->btn_6->setProperty("key",'6');
    ui->btn_7->setProperty("key",'7');
    ui->btn_8->setProperty("key",'8');
    ui->btn_9->setProperty("key",'9');
#ifndef PC_DEVICE
//    if(TScaleNew::decimalPoint()=='.')
//    {
        ui->btn_dot->setProperty("key",'.');
//    }
//    else
//    {
//        ui->btn_dot->setText(",");
//        ui->btn_dot->setProperty("key",',');
//    }
#else
    ui->btn_dot->setProperty("key",'.');
#endif
    ui->btn_ce->setProperty("key",'\b');

    connect(ui->btn_0,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_1,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_2,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_3,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_4,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_5,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_6,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_7,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_8,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_9,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_dot,SIGNAL(clicked()),this,SLOT(clickBtn()));
    connect(ui->btn_ce,SIGNAL(clicked()),this,SLOT(clickBtn()));
    mEditor = 0;
}

NewNumberKey::~NewNumberKey()
{
    delete ui;
}

void NewNumberKey::setEditor(QWidget *w)
{
    mEditor = w;
}

void NewNumberKey::clickBtn()
{
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    QChar ch;

    if (button==NULL)
        return;

    ch = button->property("key").toChar();
    QWidget *w;
    if(mEditor==0)
        w = qApp->focusWidget();
    else
        w = mEditor;

    if(w!=0)
    {
        if(w->inherits("QLineEdit"))
        {
            QLineEdit *le = (QLineEdit*)w;
            if(ch=='\b')
            {
                le->clear();
            }
            else
            {
                le->setText(le->text()+ch);
            }
        }
    }
}
